﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;

using Telerik.Windows.Controls.Charting;

namespace WaterForecast.chart
{
    /// <summary>
    /// Interaction logic for BarChartControl.xaml
    /// </summary>
    public partial class RadChartControl : UserControl
    {
        public RadChart rtChart;
        Double DefStep;
        public String HeaderTime;
        public String Descript;
        public Queue<RTChartValue> rtData;
        ChartConfig cConfig;

        public RadChartControl()
        {
            InitializeComponent();
        }

        public void ChartInit(String header, SeriesMapInfo[] SMIList, ChartConfig Config)
        {
            cConfig = Config;
            Descript = header;

            rtData = new Queue<RTChartValue>();
            rtChart = new RadChart();
            rtChart.Margin = new Thickness(5);
            rtChart.BorderThickness = new Thickness(2);
            rtChart.BorderBrush = new SolidColorBrush(Colors.Silver);

            StyleManager.SetTheme(rtChart, new SummerTheme());

            rtChart.PaletteBrushes.Clear();

            rtChart.PaletteBrushes.Add(new SolidColorBrush(Color.FromArgb(255, 255, 155, 152)));
            rtChart.PaletteBrushes.Add(new SolidColorBrush(Color.FromArgb(255, 66, 163, 189)));
            rtChart.PaletteBrushes.Add(new SolidColorBrush(Color.FromArgb(255, 238, 141, 61)));
            rtChart.PaletteBrushes.Add(new SolidColorBrush(Color.FromArgb(255, 127, 127, 127)));

            int index = 0;
            foreach (SeriesMapInfo smi in SMIList)
            {
                SeriesMapping sm = new SeriesMapping();

                sm.LegendLabel = smi.LegendLabel;

                if (Config.sDefine != null)
                {
                    sm.SeriesDefinition = Config.sDefine;
                }
                else
                {
                    sm.SeriesDefinition = new LineSeriesDefinition() { ShowPointMarks = Config.ShowPointMarks };
                }

                sm.SeriesDefinition.AxisName = smi.AxisName;
                sm.SeriesDefinition.ShowItemLabels = Config.ShowItemLables;

                if (Config.ItemLabelFormats != null && index < Config.ItemLabelFormats.Length)
                {
                    sm.SeriesDefinition.ItemLabelFormat = Config.ItemLabelFormats[index];
                }

                sm.ItemMappings.Add(new ItemMapping(smi.XFieldName, DataPointMember.XValue));
                sm.ItemMappings.Add(new ItemMapping(smi.YFieldName, DataPointMember.YValue));
                rtChart.SeriesMappings.Add(sm);
                index += 1;
            }

            rtChart.DefaultView.ChartTitle.Content = HeaderTime + Descript;
            rtChart.DefaultView.ChartTitle.Height = 32;
            rtChart.DefaultView.ChartTitle.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
            rtChart.DefaultView.ChartTitle.FontSize = 12;
            rtChart.DefaultView.ChartTitle.FontWeight = FontWeights.Normal;
            rtChart.DefaultView.ChartLegend.Header = "";
            rtChart.DefaultView.ChartLegendPosition = Dock.Bottom;
            rtChart.DefaultView.ChartArea.NoDataString = "等待获取数据";
            //rtChart.DefaultView.ChartLegend.Visibility = System.Windows.Visibility.Collapsed;
            DefStep = SMIList[0].XStep;
            rtChart.DefaultView.ChartArea.AxisX = new AxisX()
            {

                AutoRange = Config.AxisXAutoRange,
                MajorGridLinesVisibility = System.Windows.Visibility.Visible,
                StripLinesVisibility = System.Windows.Visibility.Visible,
                MaxValue = SMIList[0].XMaxValue,
                MinValue = SMIList[0].XMinValue,
                DefaultLabelFormat = SMIList[0].XLabelFormat,
                LayoutMode = Config.AxisXLayoutMode,

                IsDateTime = true,
                Step = SMIList[0].XStep,
                LabelRotationAngle = Config.LabelRotationAngle,

            };

            rtChart.DefaultView.ChartArea.AxisY = new AxisY()
            {
                MajorGridLinesVisibility = System.Windows.Visibility.Visible,
                AxisName = SMIList[0].AxisName,
                AutoRange = false,
                DefaultLabelFormat = SMIList[0].YLabelFormat,
                MaxValue = SMIList[0].YMaxValue,
                MinValue = SMIList[0].YMinValue,
                Step = SMIList[0].YStep,
            };

            if (SMIList.Count() > 1)
            {
                for (int i = 1; i < SMIList.Count(); i++)
                {
                    AxisY addY = new AxisY()
                    {
                        AxisName = SMIList[i].AxisName,
                        AutoRange = false,
                        DefaultLabelFormat = SMIList[i].YLabelFormat,
                        MaxValue = SMIList[i].YMaxValue,
                        MinValue = SMIList[i].YMinValue,
                        Step = SMIList[i].YStep,
                        Visibility = SMIList[i].ISYLableShow == true ? Visibility.Visible : Visibility.Collapsed,
                    };
                    rtChart.DefaultView.ChartArea.AdditionalYAxes.Add(addY);
                }
            }
            root.Children.Clear();
            root.Children.Add(rtChart);
        }

        #region 数据处理

        public void SetData(DateTime? xMin, DateTime? xMax, Double MaxYValue, Queue<RTChartValue> rtChartList)
        {
            this.Dispatcher.Invoke(new Action<DateTime, DateTime, Double, Queue<RTChartValue>>(FlashChart), xMin, xMax, MaxYValue, rtChartList);
        }

        void FlashChart(DateTime xMin, DateTime xMax, Double MaxYValue, Queue<RTChartValue> rtChartList)
        {
            HeaderTime = xMin.ToString("yyyy-MM-dd") + "至" + xMax.ToString("yyyy-MM-dd");
            rtChart.DefaultView.ChartTitle.Content = HeaderTime + Descript;
            rtChart.DefaultView.ChartArea.AxisX.MinValue = xMin.ToOADate();
            rtChart.DefaultView.ChartArea.AxisX.MaxValue = xMax.ToOADate();
            rtChart.DefaultView.ChartArea.AxisX.Step = DefStep;
            rtChart.ItemsSource = null;
            rtChart.ItemsSource = rtChartList;
        }

        #endregion
    }

    public class ChartConfig
    {
        public double LabelRotationAngle { get; set; }
        public Boolean ShowItemLables { get; set; }
        public Boolean ShowPointMarks { get; set; }
        public String[] ItemLabelFormats { get; set; }
        public SeriesDefinition sDefine { get; set; }
        public Boolean AxisXAutoRange { get; set; }
        public String StepType { get; set; }
        public AxisLayoutMode AxisXLayoutMode { get; set; }
        public ChartConfig()
        {
            AxisXLayoutMode = AxisLayoutMode.Between;
        }
    }

    public class RTChartValue
    {
        public DateTime Time { get; set; }
        public Double Y1Value { get; set; }
        public Double Y2Value { get; set; }
        public Double Y3Value { get; set; }
        public Double Y4Value { get; set; }
        public Double Y5Value { get; set; }
        public Double Y6Value { get; set; }
        public Double BaseLine { get; set; }

    }

    public class SeriesMapInfo
    {
        public String LegendLabel = "";
        public String XFieldName = "";
        public String YFieldName = "";

        public String AxisName = "";

        public String XLabelFormat = "";
        public Double XMaxValue = 0;
        public Double XMinValue = 0;
        public Double XStep = 0;

        public String YLabelFormat = "";
        public Double YMaxValue = 0;
        public Double YMinValue = 0;
        public Double YStep = 0;
        public Boolean ISYLableShow = true;
    }
}
